// 项目通用的vite打包配置项,供子项目使用，适用于react子项目,子项目内引用后部分内容可考虑覆盖
import legacy from "@vitejs/plugin-legacy"
import react from "@vitejs/plugin-react-swc"
import path from "path"
import { defineConfig, loadEnv } from "vite"

const proxTarget = {
  test1: "http://api.test1.logibeat.com/",
  test2: "http://api.test2.logibeat.com/",
  pre: "http://api-pre.logibeat.com/",
  prod: "https://mp.yunmai56.com/",
  sbtmanage: "https://mp.yunmai56.com/sbtmanage/", // 新后台项目
  rsbt: "https://mp.yunmai56.com/rsbt/", //旧物流云项目
  logitalk: "https://mp.yunmai56.com/logitalk/" // 运信项目
}

/**
 * @desc 有关代理配置的环境变量接口
 */
interface EnvProxyConfig {
  /**
   * @desc 旧物流云项目静态资源地址
   */
  CONFIG_PROXY_SBT: string
  /**
   * @desc 运信项目静态资源地址
   */
  CONFIG_PROXY_IM: string
  /**
   * @desc 新物流云后台静态资源地址
   */
  CONFIG_PROXY_SBTMANAGE: string

  /**
   * @desc 四种环境的代理地址。
   * @default "test1"
   */
  CONFIG_PROXY_ENV: "test1" | "test2" | "pre" | "prod"
}

export default defineConfig(({ mode }) => {
  const cwd = process.cwd()
  const proxy = loadEnv(mode, path.resolve(cwd, "env"), "CONFIG_PROXY") as Partial<EnvProxyConfig>
  // 这里需要做一下较为细致的校验为好
  let proxyEnv: EnvProxyConfig["CONFIG_PROXY_ENV"] = "test1"

  if (proxy.CONFIG_PROXY_ENV && ["test1", "test2", "pre", "prod"].includes(proxy.CONFIG_PROXY_ENV)) {
    proxyEnv = proxy.CONFIG_PROXY_ENV
  }

  return {
    plugins: [
      react(),
      legacy({
        targets: ["defaults", "not IE 11"]
      })
    ],
    envDir: path.resolve(cwd, "env"),
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          // modifyVars: {
          //   "primary-color": "#F66E2C"
          // }
          additionalData: `@import (reference) "~@logibeat/components/less/mixins/index.less"; @import (reference) "~@logibeat/components/less/variable.less";`
        }
      }
    },
    resolve: {
      alias: [
        {
          find: /^~/,
          replacement: ""
        },
        {
          find: "@",
          replacement: path.resolve(cwd, "src")
        }
      ]
    },
    build: {
      emptyOutDir: true
    },
    server: {
      open: true,
      proxy: {
        "/account": {
          target: proxTarget[proxyEnv],
          changeOrigin: true
        },
        "/insurance": {
          target: proxTarget[proxyEnv],
          changeOrigin: true
        },
        "/unicron": {
          target: proxTarget[proxyEnv],
          changeOrigin: true
        },
        "/moments": {
          target: proxTarget[proxyEnv],
          changeOrigin: true
        },
        "/bill": {
          target: proxTarget[proxyEnv],
          changeOrigin: true
        },
        "/message": {
          target: proxTarget[proxyEnv],
          changeOrigin: true
        },
        "/safe": {
          target: proxTarget[proxyEnv],
          changeOrigin: true
        },
        "/car": {
          target: proxTarget[proxyEnv],
          changeOrigin: true
        },
        "/im": {
          target: proxTarget[proxyEnv],
          changeOrigin: true
        },
        "/rsbt": {
          target: proxy.CONFIG_PROXY_SBT ?? proxTarget.rsbt,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/rsbt/, "")
        },
        "/notice": {
          target: proxTarget[proxyEnv],
          changeOrigin: true
        },
        "/netPlatform-ent": {
          changeOrigin: true,
          target: proxTarget[proxyEnv]
        },
        "/workflow": {
          changeOrigin: true,
          target: proxTarget[proxyEnv]
        },
        "/code": {
          target: proxTarget[proxyEnv],
          changeOrigin: true
        },
        "/internalSystem": {
          changeOrigin: true,
          target: proxTarget[proxyEnv]
        }
      }
    }
  }
})
